package edu.nyu.hps.assignment4;

public class Ambulance implements Comparable<Ambulance> {
	Hospital h,origH;
	People p1,p2;
	int time,currPh=0,index;
	

	public Ambulance(Hospital h) {
		this.origH = h;
		this.h = h;
		this.time = 0;
	}

	public int compareTo(Ambulance o) {
//		if (h.density < o.h.density) {
//			return -1;
//		} else if (h.density == o.h.density) {
//			return 0;
//		}
//
//		return 1;
//		if(h.casualties==o.h.casualties) return 0;
//		else if(h.casualties>o.h.casualties) return 1;
//		return -1;
//		
		if(h.density == o.h.density) return h.casualties==o.h.casualties ? 0 : (h.casualties>o.h.casualties ? -1 : 1);
		return h.density > o.h.density ? 1 : -1; 
		
//		if(h.casualties==o.h.casualties) return h.density == o.h.density ? 0 : (h.density > o.h.density ? 1 : -1);
//		return h.casualties>o.h.casualties ? 1 : -1;
	}

	public int getPh(int x1,int x2,int y1,int y2){
		int a1 = Math.min(x1, x2);
		int a2 = Math.max(x1, x2);
		int b1 = Math.min(y1, y2);
		int b2 = Math.max(y1, y2);
		int totalPh1=0,totalPh2=0;
		int i,j;
		try{		
			for(i = a1;i<a2;i++){
				totalPh1+=Computation.grid[i][b1];
			}
			
			for(i = a1;i<a2;i++){
				totalPh2+=Computation.grid[i][b2];
			}
			for(j=b1;j<b2;j++){
				
				totalPh1+=Computation.grid[a2-1][j];
			}
			for(j=b1;j<b2;j++){
				
				totalPh2+=Computation.grid[a1][j];
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return Math.max(totalPh1, totalPh2);
	}
	public void phBasedChoice(People pt1, People pt2, int distance2 ){
		int val1 = getPh(h.x,pt1.x,h.y,pt1.y);
		int val2 = getPh(h.x,pt2.x,h.y,pt2.y); 
		if(val1 + val2 > currPh){
			if(p1!=null){p1.isSaved = false;}
			if(p2!=null){p2.isSaved = false;}
			p1 = pt1;
			if(pt2 == null){
				System.out.println("I am null");
			}
			p2 = pt2;
			p1.isSaved = true;
			p2.isSaved = true;
			time += distance2;
			currPh = val1 + val2;
		}
	}
	
	public void phBasedChoice(People pt1, int distance1){
		int val1  = getPh(h.x,pt1.x,h.y,pt1.y); 
		if(val1 > currPh){
			if(p1!=null){p1.isSaved = false;}
			p1 = pt1;
			p1.isSaved  =true;
			time += distance1;
			currPh = val1;
		}
	}
	
	public void updatePh(Ambulance a, People p){
		int a1 = Math.min(a.h.x, p.x);
		int a2 = Math.max(a.h.x,p.x);
		int b1 = Math.min(a.h.y, p.y);
		int b2 = Math.max(a.h.y, p.y);
		int i,j;
		for(i=a1;i<a2;i++){
			Computation.grid[i][b1-1] += 30;
		}
		for(i=a1;i<a2;i++){
			Computation.grid[i][b2-1] += 30;
		}
		if(a1==p.x){
			for(j=b1;j<b2;j++){
				Computation.grid[a1-1][j] += 30;
			}
		}
		else if (a1==a.h.x){
			for(j=b1;j<b2;j++){
				Computation.grid[a2-1][j] += 30;
			}
		}
	}
	public String getPath(int couldSave){
		if(couldSave==1){
			int startHospital = h.index;
			h = p1.h; 
			currPh=0;
			updatePh(this,p1);
			return startHospital + " " + p1.index + " " + p1.h.index;			
		}
		else if(couldSave==2){
			int startHospital = h.index;
			if(p2 == null){
				System.out.println();
			}
			h = p2.h;
			currPh=0;
			updatePh(this,p1);
			updatePh(this,p2);
			return startHospital + " " + p1.index + " " + p2.index  + " " + p2.h.index;
		}
		else{
			return "";
		}
	}
}
